// user.js
var api = require('../../api.js');
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        contact_tel: "",
        show_customer_service: 0,
        count_data:[],
        // 钱包
        wallet:null,
        // 手机号绑定
        is_getmobile:false,
    },
    getPhoneNumber(e){
        var page = this;
        page.setData({
            is_getmobile:false
        })
        // 这里处理解密
        if (e.detail.errMsg == 'getPhoneNumber:fail user deny'){
          wx.showModal({
              title: '提示',
              showCancel: false,
              content: '未授权',
              success: function (res) {
               }
          })
        } else {
          wx.showModal({
              title: '提示',
              showCancel: false,
              content: '同意授权',
              success: function (res) { 
                wx.checkSession({
                  success:function(){
                    page.decodePhoneNum(e.detail.iv, e.detail.encryptedData);
                  },
                  fail:function(){
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp;
                    wx.setStorageSync("expiration", expiration);
                    wx.showLoading({
                      title: '登录过期',
                      duration: 3000,
                    }),

                    setTimeout(function () {
                      wx.hideLoading();
                      wx.redirectTo({
                        url: '/pages/login/login',
                      })
                    }, 2000)
                  }
                });
              }
          })
        }
    },
    cancel(){
        var page = this;
        page.setData({
            is_getmobile:false
        })
    },
    //解密encryptedData并绑定用户手机号
    decodePhoneNum(iv,data){
        var page=this;
        app.request({
            url: api.user.decodePhoneNum,
            method: "post",
            data:{
              // uid: app.globalData.Wxuser.user_id,
              token: app.globalData.token,
              encryptedData:data,
              iv:iv,
            },
            success: function (res) {
                if (res.code == 1) {
                  wx.setStorageSync("user_info", res.data);
                  app.globalData.Wxuser = res.data;
                  wx.setStorageSync("token", res.data.token);
                  app.globalData.token = res.data.token;
                  wx.showToast({
                    title: '绑定成功',
                    icon: 'success',
                    duration: 2000,
                  });
                }else{
                  wx.showToast({
                    title: res.msg,
                    image: "/images/icon-warning.png",
                    duration: 2000,
                  });
                }
            }
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      // 所有请求结果返回-1都要执行这个方法
      // app.expirate_load();
      
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var page = this;
        var user_info = app.globalData.Wxuser;
        this.setData({
            user_info: user_info,
        });
        page.getOrderCountData();
        // 当用户没有绑定手机，每次切换提醒
        if(!wx.getStorageSync("user_info").mobile){
            page.setData({
                is_getmobile:true
            })
        }
    },

    getOrderCountData: function () {
        var page = this;
        app.request({
            url: api.user.user_info,
            data:{
              // uid: app.globalData.Wxuser.user_id,
              token: app.globalData.token,
            },
            success: function (res) {
                if (res.code == 1) {
                    page.setData({
                        count_data: res.data.countData,
                        wallet:res.data.wallet,
                        is_mobile:res.data.result.mobile
                    });
                }
            }
        });
    },

    callTel: function (e) {
        var tel = e.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: tel, //仅为示例，并非真实的电话号码
        });
    },

    onPullDownRefresh:function(){
      var page = this;
      page.getOrderCountData();
    }
});